package com.idas.comp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.idas.common.Util;
import com.idas.wsdl.XmlDocumentation;

@XmlDocumentation(value = "The Address class holds the details of each address.")
public class Address {

	@XmlDocumentation(value = "The first line of the address")
	private String address1;
	@XmlDocumentation(value = "The second line of the address")
	private String address2;
	@XmlDocumentation(value = "The third line of the address or town ")
	private String town;
	@XmlDocumentation(value = "The fourth line of the address or county ")
	private String county;
	@XmlDocumentation(value = "The fifth line of the address or country ")
	private String country;
	@XmlDocumentation(value = "The sixth line of the address or postal code ")
	private String postCode;

	public Address() {
	}

	protected Address(ResultSet rs) throws SQLException {
		address1 = rs.getString("address1");
		address2 = rs.getString("address2");
		town = rs.getString("town");
		county = rs.getString("county");
		country = rs.getString("country");
		postCode = rs.getString("postcode");
	}

	public String getAddress1() {
		return address1;
	}

	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	
	public void setAddress1(Connection conn, String address1, String uuid) throws SQLException{
		String sql = "UPDATE user SET address1 = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, address1);
		ps.setString(2, uuid);
		ps.execute();
		ps.close();
		setAddress1(address1);
	}

	public String getAddress2() {
		return address2;
	}

	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	public void setAddress2(Connection conn, String address2, String uuid) throws SQLException{
		String sql = "UPDATE user SET address2 = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, address2);
		ps.setString(2, uuid);
		ps.execute();
		ps.close();
		setAddress2(address2);
	}
	
	public String getTown() {
		return town;
	}

	public void setTown(String town) {
		this.town = town;
	}
	
	public void setTown(Connection conn, String town, String uuid) throws SQLException{
		String sql = "UPDATE user SET town = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, town);
		ps.setString(2, uuid);
		ps.execute();
		ps.close();
		setTown(town);
	}

	public String getCounty() {
		return county;
	}

	public void setCounty(String county) {
		this.county = county;
	}
	
	public void setCounty(Connection conn, String county, String uuid) throws SQLException{
		String sql = "UPDATE user SET county = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, county);
		ps.setString(2, uuid);
		ps.execute();
		ps.close();
		setCounty(county);
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
	public void setCountry(Connection conn, String country, String uuid) throws SQLException{
		String sql = "UPDATE user SET country = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, country);
		ps.setString(2, uuid);
		ps.execute();
		ps.close();
		setCountry(country);
	}

	public String getPostCode() {
		return postCode;
	}

	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
	
	public void setPostCode(Connection conn, String postCode, String uuid) throws SQLException{
		String sql = "UPDATE user SET postcode = ? WHERE uuid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, postCode);
		ps.setString(2, uuid);
		ps.execute();
		ps.close();
		setPostCode(postCode);
	}
	
	public void modifyAddress(Connection conn, Address updatedAddress, String uuid)throws Exception{
		
		if(!getAddress1().equals(updatedAddress.getAddress1())){
			setAddress1(conn, updatedAddress.getAddress1(), uuid);
		}
		
		if(!getAddress2().equals(updatedAddress.getAddress2())){
			setAddress2(conn, updatedAddress.getAddress2(), uuid);
		}
		
		if(Util.requiresUpdate(getTown(), updatedAddress.getTown())){
			setTown(conn, updatedAddress.getTown(), uuid);
		}
		
		if(Util.requiresUpdate(getCounty(), updatedAddress.getCounty())){
			setCounty(conn, updatedAddress.getCounty(), uuid);
		}
		
		if(Util.requiresUpdate(getCountry(), updatedAddress.getCountry())){
			
			Country c = Country.getCountryFromCode(conn, updatedAddress.getCountry());
			
			if(null == c){
				throw new Exception("Invalid country code");
			}
			
			setCountry(conn, updatedAddress.getCountry(), uuid);
		}
		
		if(Util.requiresUpdate(getPostCode(), updatedAddress.getPostCode())){
			setPostCode(conn, updatedAddress.getPostCode(), uuid);
		}
		
	}

}